package com.ecnu.codelearn.common;

import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 *@author wang 
 *@Date 2020-9-14
 * 
 **/

@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor 
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult<T>  {
		/** 成功 */	
		public static final int SUCCESS=200;
		/**内部服务器错误**/
		public static final int FAIL=500;
	    /** 没有登录 */
	    public static final int NOT_LOGIN = 400;
	    /** 发生异常 */
	    public static final int EXCEPTION = 401;
	    /** 系统错误 */
	    public static final int SYS_ERROR = 402;
	    /** 参数错误 */
	    public static final int PARAMS_ERROR = 403;
	    /** 不支持或已经废弃 */
	    public static final int NOT_SUPPORTED = 410;
	    /** AuthCode错误 */
	    public static final int INVALID_AUTHCODE = 444;
	    /** 太频繁的调用 */
	    public static final int TOO_FREQUENT = 445;
	    /** 未知的错误 */
	    public static final int UNKNOWN_ERROR = 499;
		
		private Integer code;  
        private String msg;
        private T data;
        
        public static JsonResult fail() {
            return new JsonResult(FAIL, "请求处理失败",null);
        }
        public static JsonResult fail(String msg) {
            return new JsonResult(FAIL, msg,null);
        }
        public static JsonResult fail(Integer code,String msg) {
            return new JsonResult(code, msg,null);
        }
        public static JsonResult success() {
            return new JsonResult(SUCCESS,"请求处理成功",null);
        }
        public static JsonResult success(String msg) {
            return new JsonResult(SUCCESS,msg,null);
        }
        public static <T> JsonResult success(T data) {
            return new JsonResult<T> (SUCCESS,"请求处理成功",data);
        }
        public static <T>  JsonResult success(String msg,T data) {
            return new JsonResult<T>(SUCCESS, msg,data);
        }
        

        
        public static JsonResult err() {
            return build(EXCEPTION);
        }
        public static JsonResult err(String msg) {
            return build(EXCEPTION, msg);
        }
        
	
        public JsonResult<T> code(int code) {
            this.code = code;
            return this;
        }
        public JsonResult<T> msg(String msg) {
            this.msg = msg;
            return this;
        }
        public JsonResult<T> data(T data) {
            this.data = data;
            return this;
        }
        
        
        public static JsonResult build() {
            return new JsonResult();
        }
        public static JsonResult build(int code) {
            return new JsonResult().code(code);
        }
        public static JsonResult build(int code, String msg) {
            return new JsonResult<String>().code(code).msg(msg);
        }
        public static <T> JsonResult<T> build(int code, T data) {
            return new JsonResult<T>().code(code).data(data);
        }
        public static <T> JsonResult<T> build(int code, String msg, T data) {
            return new JsonResult<T>().code(code).msg(msg).data(data);
        }
        
        
}